package com.changgou;

import entity.IdWorker;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou *
 * @Date 2019-12-10
 * @since 1.0
 */
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.changgou.seckill.dao")
@EnableScheduling//开启任务注解
@EnableAsync//启用多线程调用注解的方式生效
public class SeckillApplication {

    public static void main(String[] args) {
        SpringApplication.run(SeckillApplication.class,args);

    }

    @Autowired
    private Environment env;


    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //设置Key存储的序列化方式为字符串
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }
    @Bean
    public IdWorker idWorker(){
        return new IdWorker(0,4);
    }


    //创建秒杀交换机
    @Bean
    public DirectExchange createSeckillDirect() {
        return new DirectExchange(env.getProperty("mq.pay.exchange.seckillorder"));
    }

    //创建秒杀队列
    @Bean
    public Queue createSeckillqueue() {
        return new Queue(env.getProperty("mq.pay.queue.seckillorder"));
    }

    //创建 秒杀的绑定
    @Bean
    public Binding createSeckillBinding() {
        return BindingBuilder.bind(createSeckillqueue()).to(createSeckillDirect()).with(env.getProperty("mq.pay.routing.seckillkey"));
    }


}
